/** @format */
var originHost = "https://m.flyco.com/";
var cookie = "";
const path = require("path");
module.exports = {
  publicPath: "./",
  configureWebpack: {
    resolve: {
      // 别名
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@assets": path.resolve(__dirname, "./src/assets"),
        "@components": path.resolve(__dirname, "./src/components"),
        "@style": path.resolve(__dirname, "./src/style"),
        "@utils": path.resolve(__dirname, "./src/utils"),
        "@sass": path.resolve(__dirname, "./src/sass"),
        "@views": path.resolve(__dirname, "./src/views")
      }
    }
  },
  devServer: {
    open: false, // 是否打开浏览器;
    hotOnly: true, // 是否热更新;

    proxy: {
      "/api": {
        // 路径中有 /api 的请求都会走这个代理 , 可以自己定义一个,下面移除即可
        target: "https://m.flyco.com", // 目标代理接口地址,实际跨域要访问的接口,这个地址会替换掉 axios.defaults.baseURL
        changeOrigin: true, // 开启代理，在本地创建一个虚拟服务端
        pathRewrite: {
          // 去掉 路径中的  /api  的这一截
          "^/api": ""
        },
        onProxyReq(proxyReq, req) {
          originHost = req.headers["x-forwarded-for"];
          // const cookie = req.headers['cookie'];
          if (cookie) {
            proxyReq.setHeader("cookie", cookie);
          }
        },
        onProxyRes(proxyRes) {
          if (proxyRes.headers["set-cookie"]) {
            console.log(cookie);
            cookie = proxyRes.headers["set-cookie"];
            // 域名信息与实际业务相关
            proxyRes.headers["set-cookie"] = proxyRes.headers["set-cookie"].map(
              v => {
                return v.replace(
                  "domain=.mufeng.me",
                  "domain=" + originHost.split(":")[0]
                );
              }
            );
          }
        }
      }
    }
  }
};
